package com.xijia.wuyouwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.UserData;
import com.xijia.wuyouwallet.pref.PrefFactory;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.StringUtils;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class IndexActiviy extends BaseActivity {

    private boolean isFirst;
    private Timer timer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        timer = new Timer();
        isFirst = PrefFactory.getUserPref().getIsFirstOpen();
        checkIsFirst();
    }
    private void checkIsFirst(){
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(isFirst)
                {
                    PrefFactory.getUserPref().setFirstOpenFalse();
                    finish();
                    startActivity(new Intent(IndexActiviy.this, GuideActivity.class));
                    overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
                }else {
                    checkIsLogin();
                }

            }
        }, 2000);
    }
    private void checkIsLogin(){
        boolean is_login = PrefFactory.getUserPref().getSelfMotionLogin();
        MyLogger.wLog().e("is_login =" + is_login);
        if (is_login) {
            String phone = PrefFactory.getUserPref().getLogonPhone();
            if (StringUtils.isEmpty(phone)) {
                return;
            }
            String pwd = PrefFactory.getUserPref().getLogonPwd();
            if (StringUtils.isEmpty(pwd)) {
                return;
            }
            login(phone, pwd);
        }else {
            finish();
            startActivity(new Intent(IndexActiviy.this, LoginActivity.class));
            overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
        }
    }
    private void login(String phone,String psd){
        Map<String,Object> map = new HashMap<>();
        map.put("login_name",phone);
        map.put("login_passwd",psd);
        XUtil.Post(UrlConfig.LOGIN_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("login result="+result);
                MyApp.userData = JSONObject.parseObject(result,UserData.class);
                if (MyApp.userData.getCode()==200){
                    finish();
                    startActivity(new Intent(IndexActiviy.this, HomeActivity.class));
                    overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
                }else {
                    MyApp.userData = null;
                    jumpToPage(LoginActivity.class);
                    finish();
                    return;
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("login onError="+ex.toString());
                MyApp.userData = null;
                jumpToPage(LoginActivity.class);
                finish();
            }
        });
    }

}
